import 'package:flutter/material.dart';
import 'package:flutter_bilibili/core/extension/double_extension.dart';
import 'package:flutter_bilibili/core/extension/int_extension.dart';
import 'package:flutter_bilibili/core/model/space_acc_info_model.dart';
import 'package:flutter_bilibili/ui/pages/mine/mine_scan_login.dart';
import 'package:flutter_bilibili/ui/shared/app_theme.dart';
import 'package:flutter_bilibili/ui/shared/image_assets.dart';
import 'package:flutter_bilibili/ui/widgets/user_level.dart';
import 'package:permission_handler/permission_handler.dart';
import '../../../core/event_bus/event_bus.dart';
import '../../widgets/advertising_row.dart';
import '../../widgets/icon_button_row.dart';
import '../../widgets/icon_route.dart';
import 'initialize_mine.dart';

class HYMineContent extends StatefulWidget {
  const HYMineContent({Key? key}) : super(key: key);

  @override
  State<HYMineContent> createState() => _HYMineContentState();
}

class _HYMineContentState extends State<HYMineContent> {
  bool loginState = false;

  ///登录状态为未登录
  late HYSpaceAccInfoModel userData;

  @override
  void initState() {
    spaceAccInfoEventBus.on<HYSpaceAccInfoModel>().listen((event) {
      setState(() {
        loginState = true;
        userData = event;
      });
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: .5,
        actions: buildMineActions(),
        bottom: PreferredSize(
          preferredSize: Size(double.infinity, 200.px),
          child: Container(
            padding: EdgeInsets.only(
                left: 20.px, right: 15.px, top: 25.px, bottom: 0.px),
            child: Column(
              children: [
                buildMineUserInfoRow(context),
                SizedBox(height: 20.px),
                buildMineUserDetailsRow(),
                SizedBox(height: 20.px),
                buildMineAppBarFooter(),
              ],
            ),
          ),
        ),
      ),
      body: SingleChildScrollView(child: buildMineContentBody()),
    );
  }

  Widget buildMineContentBody() {
    return Container(
      padding: EdgeInsets.all(15.px),
      color: Colors.white,
      child: Column(
        children: [
          loginState == true ? buildMineAdvertisingA() : Container(),
          Container(
            padding: EdgeInsets.symmetric(vertical: 15.px),
            child: HYIconButtonRow(
              iconColor: Color.fromRGBO(44, 158, 221, 1),
              iconList: [
                IconRoute(
                    assetsRoute: ImageAssets.offlineCachePNG,
                    realRoute: "/offline_cache",
                    text: "离线缓存"),
                IconRoute(
                    assetsRoute: "assets/image/icon/history_record.png",
                    realRoute: "/",
                    text: "历史记录"),
                IconRoute(
                    assetsRoute: "assets/image/icon/my_favor.png",
                    realRoute: "/",
                    text: "我的收藏"),
                IconRoute(
                    assetsRoute: "assets/image/icon/next_time_watch.png",
                    realRoute: "/",
                    text: "稍后再看"),
              ],
              size: 22.px,
            ),
          ),
          loginState == true
              ? buildMineTitleAndButton(
                  "创作中心",
                  Container(
                    decoration: BoxDecoration(
                        color: Color.fromRGBO(255, 102, 156, 1),
                        borderRadius: BorderRadius.all(
                          Radius.circular(15.px),
                        )),
                    padding: EdgeInsets.symmetric(
                      horizontal: 12.px,
                      vertical: 6.px,
                    ),
                    child: Row(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Image.asset(
                          ImageAssets.publishPNG,
                          width: 15.px,
                          height: 15.px,
                        ),
                        SizedBox(
                          width: 5.px,
                        ),
                        Text(
                          "发布",
                          style: TextStyle(
                              color: Colors.white,
                              fontSize: HYAppTheme.xxSmallFontSize,
                              fontWeight: FontWeight.bold),
                        )
                      ],
                    ),
                  ),
                )
              : Container(),
          loginState == true
              ? Container(
                  padding: EdgeInsets.symmetric(vertical: 15.px),
                  child: HYIconButtonRow(
                    iconColor: Color.fromRGBO(245, 185, 37, 1),
                    iconList: [
                      IconRoute(
                          assetsRoute:
                              "assets/image/icon/create_first_page.png",
                          realRoute: "/",
                          text: "创作首页"),
                      IconRoute(
                          assetsRoute: "assets/image/icon/file_management.png",
                          realRoute: "/",
                          text: "稿件管理"),
                      IconRoute(
                          assetsRoute: "assets/image/icon/limited_welfare.png",
                          realRoute: "/",
                          text: "限时福利"),
                      IconRoute(
                          assetsRoute: "assets/image/icon/award_activity.png",
                          realRoute: "/",
                          text: "有奖活动"),
                    ],
                    size: 22.px,
                  ),
                )
              : Container(),
          loginState == true
              ? HYIconButtonRow(
                  iconColor: HYAppTheme.norTextColors,
                  iconList: [
                    IconRoute(
                        assetsRoute: "assets/image/icon/host_management.png",
                        realRoute: "/",
                        text: "创作首页"),
                    IconRoute(
                        assetsRoute: "assets/image/icon/host_activity.png",
                        realRoute: "/",
                        text: "主播活动"),
                    IconRoute(
                        assetsRoute: "assets/image/icon/live_data.png",
                        realRoute: "/",
                        text: "直播数据"),
                    IconRoute(
                        assetsRoute: "assets/image/icon/my_live.png",
                        realRoute: "/",
                        text: "我的直播"),
                  ],
                  size: 20.px,
                )
              : Container(),
          loginState == true
              ? Container(
                  margin: EdgeInsets.symmetric(vertical: 15.px),
                  child: buildMineAdvertisingB())
              : Container(),
          loginState == true
              ? buildMineTitleAndButton(
                  "游戏中心",
                  Container(
                    padding: EdgeInsets.symmetric(
                      horizontal: 10.px,
                      vertical: 5.px,
                    ),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text(
                          "摩尔庄园x奥比岛一起过六一",
                          style:
                              TextStyle(fontSize: HYAppTheme.xxSmallFontSize),
                        ),
                        Icon(
                          Icons.arrow_forward_ios,
                          color: Color.fromRGBO(149, 149, 149, 1),
                          size: HYAppTheme.xxSmallFontSize,
                        )
                      ],
                    ),
                  ),
                )
              : Container(),
          loginState == true
              ? Container(
                  margin: EdgeInsets.symmetric(vertical: 15.px),
                  child: HYIconButtonRow(
                    iconColor: HYAppTheme.norTextColors,
                    iconList: [
                      IconRoute(
                          assetsRoute: "assets/image/icon/my_game.png",
                          realRoute: "/",
                          text: "我的游戏"),
                      IconRoute(
                          assetsRoute: "assets/image/icon/my_order.png",
                          realRoute: "/",
                          text: "我的预约"),
                      IconRoute(
                          assetsRoute: "assets/image/icon/find_game.png",
                          realRoute: "/",
                          text: "找游戏"),
                      IconRoute(
                          assetsRoute: "assets/image/icon/game_rank.png",
                          realRoute: "/",
                          text: "游戏排行榜"),
                    ],
                    size: 20.px,
                  ),
                )
              : Container(),
          buildMineTitleAndButton("推荐服务", const Center()),
          Container(
            margin: EdgeInsets.symmetric(vertical: 15.px),
            child: HYIconButtonRow(
              iconColor: HYAppTheme.norTextColors,
              iconList: [
                IconRoute(
                    assetsRoute: "assets/image/icon/my_class.png",
                    realRoute: "/",
                    text: "我的课程"),
                IconRoute(
                    assetsRoute: "assets/image/icon/free_flow_service.png",
                    realRoute: "/",
                    text: "免流量服务"),
                IconRoute(
                    assetsRoute: "assets/image/icon/sexy_clothes.png",
                    realRoute: "/",
                    text: "个性装扮"),
                IconRoute(
                    assetsRoute: "assets/image/icon/my_walt.png",
                    realRoute: "/",
                    text: "我的钱包"),
              ],
              size: 20.px,
            ),
          ),
          HYIconButtonRow(
            iconColor: HYAppTheme.norTextColors,
            iconList: [
              IconRoute(
                  assetsRoute: "assets/image/icon/vip_mail_center.png",
                  realRoute: "/",
                  text: "会员购中心"),
              IconRoute(
                  assetsRoute: "assets/image/icon/recommand_award.png",
                  realRoute: "/",
                  text: "推荐有奖"),
              IconRoute(
                  assetsRoute: "assets/image/icon/talk_center.png",
                  realRoute: "/",
                  text: "社区中心"),
              IconRoute(
                  assetsRoute: "assets/image/icon/bilibili_benefit.png",
                  realRoute: "/",
                  text: "哔哩哔哩公益"),
            ],
            size: 20.px,
          ),
          Container(
            margin: EdgeInsets.symmetric(vertical: 15.px),
            child: HYIconButtonRow(
              iconColor: HYAppTheme.norTextColors,
              iconList: [
                IconRoute(
                    assetsRoute: "assets/image/icon/eletric_benefit.png",
                    realRoute: "/",
                    text: "充能领福利"),
                IconRoute(
                    assetsRoute: "assets/image/icon/gas_station.png",
                    realRoute: "/",
                    text: "能量加油站"),
                loginState == true
                    ? IconRoute(assetsRoute: "", realRoute: "/", text: "")
                    : IconRoute(
                        assetsRoute: "assets/image/icon/red_package.png",
                        realRoute: "/",
                        text: "邀好友赚红包"),
                loginState == true
                    ? IconRoute(assetsRoute: "", realRoute: "/", text: "")
                    : IconRoute(
                        assetsRoute: "assets/image/icon/game_center.png",
                        realRoute: "/",
                        text: "游戏中心"),
              ],
              size: 20.px,
            ),
          ),
          buildMineTitleAndButton("更多服务", const Center()),
          Container(
            margin: EdgeInsets.symmetric(horizontal: 10.px),
            child: Column(
              children: [
                Container(
                  margin: EdgeInsets.only(top: 15.px),
                  child: buildMineMoreServiceItem(
                      "assets/image/icon/Contact_service.png", "联系客服"),
                ),
                loginState == true
                    ? Container(
                        margin: EdgeInsets.only(top: 15.px),
                        child: buildMineMoreServiceItem(
                            "assets/image/icon/listen_video.png", "听视频"),
                      )
                    : Container(),
                Container(
                  margin: EdgeInsets.only(top: 15.px),
                  child: buildMineMoreServiceItem(
                      "assets/image/icon/teens_mode.png", "青少年守护"),
                ),
                Container(
                  margin: EdgeInsets.only(top: 15.px),
                  child: buildMineMoreServiceItem(
                      "assets/image/icon/settings.png", "设置"),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget buildMineMoreServiceItem(String icon, String text) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            Image.asset(
              icon,
              width: 20.px,
              height: 20.px,
              color: HYAppTheme.norTextColors,
            ),
            SizedBox(
              width: 10.px,
            ),
            Text(
              text,
              style: TextStyle(
                  fontSize: HYAppTheme.smallFontSize, color: Colors.black),
            ),
          ],
        ),
        Icon(
          Icons.arrow_forward_ios,
          size: HYAppTheme.xxSmallFontSize,
          color: HYAppTheme.norGrayColor,
        ),
      ],
    );
  }

  scan() async {
    var status = await Permission.camera.request();
    if (status.isGranted) {
      Navigator.of(context).pushNamed(HYMineScanLogin.routeName);
    }
  }

  List<Widget> buildMineActions() {
    return [
      IconButton(
          onPressed: () => print("bilibili_connect"),
          icon: Image.asset(
            ImageAssets.bilibiliConnectPNG,
            width: iconSize,
            height: iconSize,
          )),
      IconButton(
          onPressed: () {
            scan();
          },
          icon: Image.asset(
            ImageAssets.scanPNG,
            width: iconSize,
            height: iconSize,
          )),
      IconButton(
          onPressed: () => print("clothes"),
          icon: Image.asset(
            ImageAssets.clothesPNG,
            width: iconSize,
            height: iconSize,
          )),
      IconButton(
          onPressed: () => print("dark_model"),
          icon: Image.asset(
            ImageAssets.darkModelPNG,
            width: iconSize,
            height: iconSize,
          )),
    ];
  }

  ///动态关注粉丝API  https://api.bilibili.com/x/web-interface/nav/stat 需要登录验证，这里就直接写死数据
  Widget buildMineUserDetailsRow() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: [
        buildUserInfo(loginState == false ? "—" : "72", "动态"),
        Stack(
          alignment: AlignmentDirectional.center,
          children: [
            Container(
              child: buildUserInfo(loginState == false ? "—" : "398", "关注"),
            ),
            Container(
              height: 20.px,
              padding: EdgeInsets.symmetric(horizontal: 60.px),
              decoration: BoxDecoration(
                  border: Border.symmetric(vertical: BorderSide(width: .2.px))),
            ),
          ],
        ),
        buildUserInfo(loginState == false ? "—" : "18", "粉丝"),
      ],
    );
  }

  Widget buildUserInfo(String num, String text) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Text(
          num,
          style: TextStyle(
              color: Colors.black, fontSize: HYAppTheme.xSmallFontSize),
        ),
        Text(
          text,
          style: TextStyle(
              color: HYAppTheme.norGrayColor,
              fontSize: HYAppTheme.xxSmallFontSize),
        ),
      ],
    );
  }

  Widget buildMineUserInfoRow(BuildContext context) {
    return loginState == true
        ? Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Container(
                height: 50.px,
                width: 50.px,
                child: CircleAvatar(
                  backgroundImage: NetworkImage(userData.face),
                ),
              ),
              SizedBox(
                width: 20.px,
              ),
              Expanded(
                child: Stack(
                  children: [
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            Text(
                              userData.name,
                              style: TextStyle(
                                  color: Colors.black,
                                  fontSize: HYAppTheme.smallFontSize),
                            ),
                            HYUserLevel(level: userData.level)
                          ],
                        ),
                        SizedBox(
                          height: 5.px,
                        ),
                        Container(
                          padding: EdgeInsets.symmetric(
                              horizontal: 2.px, vertical: 0),
                          child: Text(
                            "正式会员",
                            style: TextStyle(
                                color: HYAppTheme.norTextColors,
                                fontSize: HYAppTheme.xxxSmallFontSize),
                          ),
                          decoration: BoxDecoration(
                              border:
                                  Border.all(color: HYAppTheme.norTextColors),
                              borderRadius:
                                  BorderRadius.all(Radius.circular(3.px))),
                        ),
                        SizedBox(
                          height: 5.px,
                        ),
                        Text(
                          "B币: 0.0 硬币: ${userData.coins}",
                          style: TextStyle(
                              color: Color.fromRGBO(149, 149, 149, 1),
                              fontSize: HYAppTheme.xxSmallFontSize),
                        )
                      ],
                    ),
                    Positioned(
                      right: 0,
                      top: 0,
                      bottom: 0,
                      child: Row(
                        children: [
                          Text(
                            "空间",
                            style: TextStyle(
                                color: HYAppTheme.norGrayColor,
                                fontSize: HYAppTheme.xxSmallFontSize),
                          ),
                          Icon(
                            Icons.arrow_forward_ios,
                            size: HYAppTheme.xxSmallFontSize,
                            color: HYAppTheme.norGrayColor,
                          )
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ],
          )
        : Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Container(
                height: 50.px,
                width: 50.px,
                child: CircleAvatar(
                  backgroundImage: AssetImage(ImageAssets.unLoginPNG),
                ),
              ),
              SizedBox(
                width: 20.px,
              ),
              Expanded(
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      "点击登录",
                      style: TextStyle(
                          fontSize: HYAppTheme.smallFontSize,
                          color: Colors.black,
                          fontWeight: FontWeight.bold),
                    ),
                    Icon(
                      Icons.arrow_forward_ios,
                      size: HYAppTheme.xxSmallFontSize,
                      color: HYAppTheme.norGrayColor,
                    )
                  ],
                ),
              ),
            ],
          );
  }

  Widget buildMineAppBarFooter() {
    return Container(
      width: double.infinity,
      padding: EdgeInsets.symmetric(vertical: 10.px, horizontal: 15.px),
      decoration: BoxDecoration(
        color: Color.fromRGBO(255, 236, 240, 1),
        borderRadius: BorderRadius.vertical(top: Radius.circular(4.px)),
        border:
            Border.all(color: Color.fromRGBO(177, 143, 157, 1), width: .5.px),
      ),
      child: Stack(
        children: [
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                "成为大会员",
                style: TextStyle(
                    color: Color.fromRGBO(236, 114, 153, 1),
                    fontSize: HYAppTheme.smallFontSize),
              ),
              Text(
                "了解更多权益",
                style: TextStyle(
                    color: Color.fromRGBO(215, 120, 150, 1),
                    fontSize: HYAppTheme.xxSmallFontSize),
              )
            ],
          ),
          Positioned(
            right: 0.px,
            top: 0,
            bottom: 0,
            child: Icon(
              Icons.arrow_forward_ios,
              size: HYAppTheme.xxSmallFontSize,
              color: Color.fromRGBO(217, 121, 151, 1),
            ),
          )
        ],
      ),
    );
  }

  Widget buildMineAdvertisingA() {
    return HYAdvertisingRow(
      image: "assets/image/icon/broadcast.png",
      title: "守护未成年专项与4月社区治理汇报",
      rightBtn: Icon(
        Icons.close,
        size: 20.px,
        color: HYAppTheme.norGrayColor,
      ),
    );
  }

  Widget buildMineAdvertisingB() {
    return HYAdvertisingRow(
      image: "assets/image/icon/flag.png",
      title: "你有一份儿童节礼物待领取",
      rightBtn: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Text(
            "立即领取",
            style: TextStyle(
              color: HYAppTheme.norTextColors,
              fontSize: HYAppTheme.xxSmallFontSize,
            ),
          ),
          Icon(
            Icons.arrow_forward_ios,
            color: HYAppTheme.norTextColors,
            size: HYAppTheme.xxSmallFontSize,
          )
        ],
      ),
    );
  }

  Widget buildMineTitleAndButton(String title, Widget button) {
    return Container(
      padding: EdgeInsets.only(top: 5.px, bottom: 0, left: 10.px, right: 5.px),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            title,
            style: TextStyle(
                color: Colors.black,
                fontSize: HYAppTheme.xSmallFontSize,
                fontWeight: FontWeight.bold),
          ),
          button
        ],
      ),
    );
  }
}
